home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Best of MacTutor - S…e Code for Volumes 1 to 5
/
The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin
/
Source Code
/
#46 (Jul 89)
/
XCMD Source
/
filepeek.c
< prev
next >
Wrap
C/C++ Source or Header
|
1989-05-03
|
5KB
|
232 lines
/********************************/
/* File: FilePeek.c */
/* */
/* Given the sector index into */
/* an existing file, read the */
/* sector in and "dump" it to */
/* the screen */
/* */
/* Paramters: */
/* param0 = file reference num */
/* ( file is open ) */
/* param1 = sector number */
/* (1 sector = 512 bytes) */
/* ---------------------------- */
/* To Build: */
/* */
/* (1) Create a project using */
/* this file as well as the */
/* XCMD.Glue.c file. (Set */
/* project type to XCMD (or */
/* XFCN) from the Project menu. */
/* */
/* (2) Bring the project up to */
/* date. */
/* */
/* (3) Build Code Resource. */
/* */
/* (4) Use ResEdit to copy the */
/* resource to your stack. */
/********************************/
#include <MacTypes.h>
#include <OSUtil.h>
#include <MemoryMgr.h>
#include <FileMgr.h>
#include <ResourceMgr.h>
#include <pascal.h>
#include <strings.h>
#include "HyperXCmd.h"
#include "HyperUtils.h"
#define SECTOR 512 /*** size of input buffer ***/
#define NUMROWS 8 /*** across each line ***/
#define NUMLINES 32 /*** lines of data ***/
#define NUMBYTES 16 /*** #bytes per row of data ***/
#define SPACE 0x020 /*** the space character ***/
long paramtoNum();
void appendChar();
void CopyStrToHandle();
char *CopyAscii();
pascal void main( paramPtr )
XCmdBlockPtr paramPtr;
{
short err;
short fref;
short lc; /* line count */
short rc; /* row count */
long cnt;
long blk; /* sector number */
Handle outData;
short *rPtr; /* per row */
char *cPtr; /* for ASCII */
char *aPtr; /* points to asciival */
char *buf;
char asciiStr[32];
char curntLine[256];
Str31 numString;
outData = 0L;
if( paramPtr->paramCount == 2 ){ /*** expect two parameters ***/
/*** (1) Get our input parameters ***/
fref = (short)paramtoNum( paramPtr, 0 );
blk = paramtoNum( paramPtr, 1 );
/*** (2) Read a buffer of data ***/
blk = blk * SECTOR;
err = SetFPos( fref, fsFromStart, blk );
if ( !err ){
cnt = SECTOR;
/*** We need to keep this data locked ***/
/*** Since we can;t trust callbacks not ***/
/*** to move stuff, we allocate the ***/
/*** buffer as non-relocatable. ***/
buf = NewPtr( cnt );
err = FSRead( fref, &cnt, buf );
if ( err != noErr && err != eofErr ){
paramPtr->returnValue = 0L;
return;
}
}
/*** result is returned to hypercard as ***/
/*** a null terminated string ***/
outData = NewHandle( 0L );
/*** (3) Start filling the output buffer ***/
*curntLine = '\0';
/*** first the number of bytes read in ***/
NumToHex( paramPtr, cnt, 4 , &numString );
appendChar( PtoCstr( (char *)&numString), '\r' );
CopyStrToHandle( &numString, outData );
/*** point to the input data ***/
rPtr = (short *)buf;
for( lc = 0; lc < NUMLINES; ++lc ){
*curntLine = '\0';
/*** blk is the sector address ***/
NumToHex( paramPtr, blk, 6, &numString );
PtoCstr( (char *)&numString );
strcat( curntLine, &numString );
appendChar( curntLine, ':' );
appendChar( curntLine, SPACE );
aPtr = asciiStr;
blk += NUMBYTES;
for( rc = 0; rc < NUMROWS; ++rc ){
/*** convert eight shorts per row ***/
/*** if we overrun the buffer, draw ***/
/*** whatever data follows it... ***/
NumToHex( paramPtr, (long)*rPtr, 4 , &numString);
strcat( curntLine, PtoCstr( (char *)&numString));
appendChar( curntLine, ' ' );
cPtr = (char *)rPtr;
aPtr = CopyAscii( aPtr, *cPtr++ );
aPtr = CopyAscii( aPtr, *cPtr++ );
rPtr++;
}
*aPtr = '\0'; /*** terminate the ascii data ***/
strcat( curntLine, asciiStr );
appendChar( curntLine, '\r' );
/*** move line into output buffer ***/
CopyStrToHandle( curntLine, outData );
}
DisposPtr( buf );
}
cnt = GetHandleSize( outData );
*(*outData + cnt) = '\0';
paramPtr->returnValue = outData;
}
long paramtoNum( paramPtr, i )
XCmdBlockPtr paramPtr;
short i;
/************************
* Given a handle to an input
* argument in the paramBlk
* return an integer representation
* of the data.
*
************************/
{
Str31 theStr;
HLock( paramPtr->params[ i ] );
ZeroToPas( paramPtr, *(paramPtr->params[ i ]), &theStr );
HUnlock( paramPtr->params[ i ] );
return( StrToLong( paramPtr, &theStr ) );
}
void appendChar( theStr, theChar )
char *theStr;
char theChar;
/************************
* append the character passed
* to the end of the string
************************/
{
long len = strlen( theStr );
char *theEnd;
theEnd = theStr + len;
*theEnd++ = theChar;
*theEnd = '\0';
}
char *CopyAscii( outStr, theChar )
char *outStr;
char theChar;
/************************
* if the character passed
* in the input stream is a
* printing character, append
* it to the output string,
* otherwise, append the '.'
*
* return the update output
* string.
************************/
{
if ( theChar >= SPACE && theChar <= 0x0D8 )
*outStr++ = theChar;
else
*outStr++ = '.';
return( outStr );
}
void CopyStrToHandle( theStr, hand )
char *theStr;
Handle hand;
/************************
* Copy the input data to the
* output handle.
*
************************/
{
long cnt = strlen( theStr );
long oldSize = GetHandleSize( hand );
SetHandleSize( hand, oldSize + cnt );
BlockMove( theStr, *hand + oldSize, cnt );
}